/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lwjglbase;

import java.awt.Color;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.util.ArrayList;
import java.util.StringTokenizer;
import org.lwjgl.BufferUtils;
import org.lwjgl.input.*;
import org.lwjgl.opengl.Display;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.util.glu.GLU.*;
import org.newdawn.slick.opengl.Texture;

public class Editor implements RoomBase {

    Texture firstSprite;
    ArrayList<Cube> cubes = new ArrayList<Cube>();
    Button button;
    int mouseX = 0;
    int mouseY = 0;
    Camera cam = new Camera(0f, 0f, -5f, 90f, 90f);
    EditGui gui = new EditGui();
    boolean prevMouseLeft, prevMouseRight, currentMouseLeft, currentMouseRight;
    final int GRID_SIZE = 10;
    Grid grid = new Grid(GRID_SIZE);
    Cube[][][] cubeGrid = new Cube[GRID_SIZE][GRID_SIZE][GRID_SIZE];
    
    public Editor() {
        initLights();
        
        Cube.init();
        Sprite.init();
        Player.init();
        
        cubes.add(new Cube(5f,5f,5f));
    }

    private void initLights() {
        for (int light = GL_LIGHT0; light < GL_LIGHT7; light++) {
            glDisable(light); //disable all lights that are not out own
        }
        Light dir1 = new Light(GL_LIGHT0, Light.DIRECTION, 1f, 1f, 1f, Color.WHITE, 0f);
        Light dir2 = new Light(GL_LIGHT1, Light.DIRECTION, -1f, -1f, 0f, Color.GRAY, 0f);
        
        float[] white = {.4f,.4f,.4f,1f};
        glLightModel(GL_LIGHT_MODEL_AMBIENT, Utils.createFB(white));
    }
    
    private void loadLevel(String in) {
        try {
            // Read from in and parse cubes
            String parse = "";                                                               //Path to levels
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(LWJGLbase.LEVELS_PATH + in)));
            while ((parse = br.readLine()) != null) {
                int[] sides = new int[6];
                StringTokenizer st = new StringTokenizer(parse, " ");
                int x = Integer.parseInt(st.nextToken());
                int y = Integer.parseInt(st.nextToken());
                int z = Integer.parseInt(st.nextToken());
                for (int i = 0; i < sides.length; i++) {
                    sides[i] = Integer.parseInt(st.nextToken());
                }
                cubes.add(new Cube(x, y, z, sides));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void update(long delta) {
        //Handle input
        mouseX = Mouse.getX();
        mouseY = Display.getHeight() - Mouse.getY(); //0,0 at top instaed of bottom
        currentMouseLeft = Mouse.isButtonDown(0);
        currentMouseRight = Mouse.isButtonDown(1);


        if (prevMouseLeft == false && currentMouseLeft == true) {// Mouse(left) pressed
            if(mouseX > Display.getHeight()) { //clicked in gui
                float mx = mouseX - Display.getHeight();
                float my = mouseY;
                gui.handleClicks(mx, my);
            } else { //clicked in 3d area
                if(gui.mode == EditGui.ADD_MODE) {
                    FloatBuffer modelview = BufferUtils.createFloatBuffer(16);
                    FloatBuffer projection = BufferUtils.createFloatBuffer(16);
                    IntBuffer viewport = BufferUtils.createIntBuffer(16);
                    FloatBuffer result = BufferUtils.createFloatBuffer(3);
                    FloatBuffer zBuffer = BufferUtils.createFloatBuffer(1);
                    glReadPixels(mouseX, mouseY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, zBuffer);
                    float z = zBuffer.get(0);
                    
                    glGetFloat(GL_MODELVIEW_MATRIX, modelview);
                    glGetFloat(GL_PROJECTION_MATRIX, projection);
                    glGetInteger(GL_VIEWPORT, viewport);
                    System.out.println(z);
                    gluUnProject(mouseX, Display.getHeight()-mouseY, z, modelview, projection, viewport, result);
                    System.out.println(result.get(0) + " " + result.get(1) + " " + result.get(2));
                    cubes.add(new Cube(result.get(0),result.get(1),result.get(2)));
                }
            }
        } else if (prevMouseLeft == true && currentMouseLeft == false) { // Mouse(left) released
        } else if (prevMouseRight == false && currentMouseRight == true) { // Mouse(right) pressed
            if (mouseX < Display.getHeight()) {
                Mouse.setGrabbed(true);
            }
        } else if (prevMouseRight == true && currentMouseRight == false) { // Mouse(right) released
            Mouse.setGrabbed(false);
        }
        if (currentMouseRight) {
            if (mouseX < Display.getHeight()) {
                cam.rotate(Mouse.getDX(), Mouse.getDY());
            }
        }
        cam.move();
        prevMouseLeft = currentMouseLeft;
        prevMouseRight = currentMouseRight;
    }

    @Override
    public void draw(long delta) {
        //draw main screen
        glEnable(GL_LIGHTING);
        glEnable(GL_NORMAL_ARRAY);
        glViewport(0, 0, Display.getHeight(), Display.getHeight());
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(90.0f, Display.getWidth() / Display.getHeight(), .1f, 1000f);
        cam.look();
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        for (Cube cube : cubes) {
            cube.draw();
        }
        grid.draw();
        //tester.draw();

        //draw sidescreen
        glDisable(GL_LIGHTING);
        glDisable(GL_NORMAL_ARRAY);
        glViewport(Display.getHeight(), 0, Display.getWidth() - Display.getHeight(), Display.getHeight());
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0f, 400f, 800f, 0f, -1f, 1f);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        //draw gui
        gui.render();
    }
}
